﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace BaseThing.Engine
{
    class GameObject
    {
        public bool alive;
        public List<Sprite> images = new List<Sprite>();
        private GameObject parent = null;
        private List<GameObject> children = new List<GameObject>();
        private Vector2 m_vPosition;
        private Vector2 m_vDirection;
        private int m_iSpeed;


        public GameObject()
        {
            this.Position = Vector2.Zero;
            this.Direction = Vector2.Zero;
            this.Speed = 1;
            this.alive = true;
        }
        public GameObject(Vector2 l_vPosition, Vector2 l_vDirection, int l_iSpeed, string assetName, int cols, int rows, int fps)
        {
            //texture,cols,rows,frames per seconds
            Sprite sprite = new Sprite(assetName, cols, rows, fps);
            sprite.Position = l_vPosition;

            this.Position = l_vPosition;
            this.Direction = l_vDirection;
            this.Speed = l_iSpeed;
            this.images.Add(sprite);

            alive = true;
        }

        public void AddSprite(Sprite l_sprite)
        {
            this.images.Add(l_sprite);
        }

        public void SetParent(GameObject parent)
        {
            this.parent = parent;
        }

        public void AddChild(GameObject child)
        {
            child.SetParent(this);
            children.Add(child);
        }

        public void Remove(GameObject child)
        {
            children.Remove(child);
        }

        public List<GameObject> Children
        {
            get { return children; }
        }
        
        public GameObject Parent
        {
            get { return parent; } 
        }

        public Vector2 Position
        {
            set { m_vPosition = value; }
            get { return m_vPosition; }
        }

        public Vector2 Direction
        {
            set 
            { 
                value.Normalize();
                m_vDirection = value;
            }
            get { return m_vDirection; }
        }

        public int Speed
        {
            set { m_iSpeed = value; }
            get { return m_iSpeed; }
        }


        public void Move(Vector2 l_Direction)
        {
            Position = Position + l_Direction * Speed;
            Direction = l_Direction;
            
            foreach (Sprite image in images)
            {
                image.Position = Position;
            }

            foreach (GameObject child in children)
            {
                child.Move(l_Direction);
            }
        }

        public virtual void Update(GameTime gameTime) 
        {
            foreach (Sprite image in images)
            {
                image.Update(gameTime);
            }

            foreach (GameObject child in children)
            {
                child.Update(gameTime);
            }
        }

        
        public virtual void Draw(SpriteBatch spriteBranch)
        {
            foreach (Sprite image in images)
            {
                image.Draw(spriteBranch);
            }

            foreach (GameObject child in children)
            {
                child.Draw(spriteBranch);
            }
        }


        #region COLLISION
/*
        public Color getRGB(int x, int y)
        {
            Color rgb = Color.Transparent;
            int pos;

            if (imageMask != null && textureData == null)
            {
                textureData = new Color[imageMask.Width * imageMask.Height];
                imageMask.GetData(textureData);

            }
            else
            {
                imageMask = image;
            }
            // pos = (int)(Math.Abs((x - this.x)) + (Math.Abs((y - this.y)) * this.getWidth()));
            pos = x + (y * this.getWidth());
            rgb = textureData[pos];

            return rgb;
        }
        bool collision(GameObject a, GameObject b)
        {

            Rectangle img1Bounds = new Rectangle(a.x, a.y, a.image.Width, a.image.Height);
            Rectangle img2Bounds = new Rectangle(b.x, b.y, b.image.Width, b.image.Height);

            if (img1Bounds.Intersects(img2Bounds)) return true;

            return false;
        }

        bool collisionPerPixel(GameObject a, GameObject b)
        {

            // Find the bounds of the rectangle intersection
            int top = Math.Max(a.Getbo, b.getRect().Top);
            int bottom = Math.Min(a.getRect().Bottom, b.getRect().Bottom);
            int left = Math.Max(a.getRect().Left, b.getRect().Left);
            int right = Math.Min(a.getRect().Right, b.getRect().Right);


            rect.X = left;
            rect.Y = top;
            rect.Width = right;
            rect.Height = bottom;

            
            // Check every point within the intersection bounds
            for (int y = top; y < bottom; y++)
            {
                for (int x = left; x < right; x++)
                {
                    // Get the color of both pixels at this point
                    Color colorA = a.getPixels()[(x - a.getRect().Left) +
                                         (y - a.getRect().Top) * a.getRect().Width];
                    Color colorB = b.getPixels()[(x - b.getRect().Left) +
                                         (y - b.getRect().Top) * b.getRect().Width];


                    text = "Color A: " + colorA.ToString() + " Color B: " + colorB.ToString() + "\n" + txt;

                    // If both pixels are not completely transparent,
                    if (colorA.A != 0 && colorB.A != 0)
                    {
                        // then an intersection has been found
                        return true;
                    }
                }
            }

            return false;
        }

        bool collisionDureza(Color color, GameObject obj, GameObject imgDureza)
        {
            int add = 0;

            int x = obj.x + add;
            int y = obj.y + add;
            int w = obj.getWidth() + add;
            int h = obj.getHeight() + add;


            if (imgDureza.getRGB(x, y) == color ||
                    imgDureza.getRGB(x + w, y) == color ||
                    imgDureza.getRGB(x, y + h) == color ||
                    imgDureza.getRGB(x + w, y + h) == color)
            {
                return true;
            }




            return false;
        }
        public Color[] getPixels()
        {
            if (textureData == null)
            {
                textureData = new Color[image.Width * image.Height];
                image.GetData(textureData);
            }
            return textureData;
        }
        */
        #endregion
    }
}
